import {createRouter, createWebHistory} from "vue-router";
import {useUserStore} from "@/stores/user";

// @ts-ignore

const routers=[
    {
        path: '/',
        name: 'Home',
        meta: { public: true },
        // @ts-ignore
        component: () => import('@/pages/Home.vue'),
        children:[
            {
                path:'allgz',
                name:'AllGz',
                meta: { public: false},
                // @ts-ignore
                component: () => import('@/components/AllGuanZhu.vue'),
            },
            {
                path:'notice',
                name:'Notice',
                meta: { public: false},
                // @ts-ignore
                component: () => import('@/components/Notice.vue'),
            },
            {
                path:'',
                name:'Post',
                // @ts-ignore
                component: () => import('@/components/Post.vue'),
            },
            {
                path:'poster',
                name:'poster',
                meta: { public: true},
                // @ts-ignore
                component: () => import('@/components/Poster.vue'),
            },
            {
                path:'hot',
                name:'hot',
                // @ts-ignore
                component: () => import('@/components/wzHotSearch.vue'),
            },
            {
                path:'hoet',
                name:'Hoet',
                meta: { public: true},
                // @ts-ignore
                component: () => import('@/pages/hoet.vue'),
            },
            {
                path:'new',
                name:'New',
                // @ts-ignore
                component: () => import('@/components/NewPost.vue'),
            },
            {
                path:'hotPost',
                name:'HotPost',
                // @ts-ignore
                component: () => import('@/components/HotPost.vue'),
            },

            {
                path:'search',
                name:'search',
                // @ts-ignore
                component: () => import('@/components/search.vue'),
            },

        ]
    },
    {
        path: '/yycsh',
        name: 'yycsh',
        meta: { public: true },
        // @ts-ignore
        component: () => import('@/pages/LogSign.vue'),
        children:[
            {
                path:'',
                name:'Login',
                // @ts-ignore
                component: () => import('@/pages/Login.vue'),
                meta: { public: true },
            },
            {
                path:'sign',
                name:'Sign',
                // @ts-ignore
                component: () => import('@/pages/Register.vue'),
                meta: { public: true },
            },
            {
                path:'forget',
                name:'Forget',
                // @ts-ignore
                component: () => import('@/pages/ForgetRegister.vue'),
                meta: { public: true },
            },
        ]
    },
    {
        path: '/video',
        name: 'Video',
        meta: { public: true },
        // @ts-ignore
        component: () => import('@/pages/Video.vue'),
        children:[
            {
                path:'',
                name:'TuiJian',
                // @ts-ignore
                component: () => import('@/pages/video/TuiJian.vue'),
            },
            {
                path:'open',
                name:'Open',
                // @ts-ignore
                component: () => import('@/pages/video/OpenVideo.vue'),
            },
            {
                path:'gz',
                name:'Gz',
                // @ts-ignore
                component: () => import('@/pages/video/gz.vue'),
            },
            {
                path:'hotvideo',
                name:'HotVideo',
                // @ts-ignore
                component: () => import('@/pages/video/Hot.vue'),
            },
        ]
    },
    {
        path:'/admin',
        name:'Admin',
        meta: { public: false },
        // @ts-ignore
        component: () => import('@/pages/Admin/AdminIndex.vue'),
        children:[
            {
                path:'user',
                name:'User',
                meta: { public: true },
                // @ts-ignore
                component: () => import('@/pages/AdministratorPage.vue'),
            },
            {
                path:'post',
                name:'Posts',
                meta: { public: true },
                // @ts-ignore
                component: () => import('@/components/PostMS.vue'),
            },
            {
                path:'yhfx',
                name:'Yhfx',
                meta: { public: true },
                // @ts-ignore
                component: () => import('@/pages/AnalaysisUserPage.vue'),
            },
            {
                path:'postfx',
                name:'Postfx',
                meta: { public: true },
                // @ts-ignore
                component: () => import('@/components/PostAnalysis.vue'),
            },
        ]
    },
    {
        path:'/chat',
        name:'Chat',
        meta: { public: false },
        // @ts-ignore
        component: () => import('@/components/WebSocketComponent.vue'),
    },
    {
        path:'/mychat',
        name:'MyChat',
        meta: { public: false },
        // @ts-ignore
        component: () => import('@/pages/ChatPage.vue'),
    },


]
const router = createRouter({
    history: createWebHistory(),
    routes: routers
})

router.beforeEach((to, from, next) => {
    const userStore = useUserStore()
    // 1. 如果是管理员路由，检查 glyIsLoggedIn
    if (to.name === 'Admin') {
        if (userStore.glyIsLoggedIn) {
            next(); // 已登录，放行
        } else {
            next({ name: 'Login' }); // 未登录，跳转登录
        }
    }
    // 2. 其他非公开路由，检查 isLoggedIn
    else if (!to.meta.public && !userStore.isLoggedIn) {
        next({ name: 'Login' });
    }
    // 3. 其他情况放行
    else {
        next();
    }


})
export default router